<template>
  <div class="content">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      size="small"
      class="zb-form"
      label-position="left"
    >

      <el-form-item
        label="激活码"
        prop="activationCode"
      >
        <el-input v-model="ruleForm.activationCode"></el-input>
      </el-form-item>

      <el-form-item
        size="medium"
        class="action-box"
        label-width="0px"
      >
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >下一步<i class="el-icon-arrow-right el-icon--right"></i></el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import utils from "@/mixins/utils.js";
export default {
  props: ["OldActivationCode"],
  minxins: [utils],
  data() {
    const self = this;
    async function checkActivationCode(rule, value, callback) {
      if (value === "") {
        callback(new Error("激活码不能为空"));
      } else {
        const isVaid = await self.checkActiveCode(value).catch(err => {
          callback(new Error(err));
        });
        if (!isVaid) {
          callback(new Error(""));
        } else {
          callback();
        }
      }
    }
    return {
      ruleForm: {
        activationCode: ""
      },
      rules: {
        activationCode: [
          { required: true, validator: checkActivationCode, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    if (this.OldActivationCode) {
      this.ruleForm.activationCode = this.OldActivationCode;
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$emit(
            "setFormData",
            "activationCode",
            this.ruleForm.activationCode
          );
          this.$emit("goNextStep");
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    checkActiveCode(val) {
      return new Promise((resolve, reject) => {
        API.fetchGet("/operatorPark/checkValidateCode", {
          activationCode: val
        }).then(
          res => {
            resolve(true);
          },
          err => {
            reject(err.data.message);
          }
        );
      });
    }
  }
};
</script>

<style lang="less" scoped>
@import "../FormContent.less";
</style>
